import java.util.Arrays;

public class MergeTest {
    public static void main(String[] args) {
        int[] a=new int[]{3,1,2,7,4,5,6};
        MergeTest mt=new MergeTest();
        int[] temp=new int[a.length];
        mt.mergeSort(a,0,a.length-1,temp);
        System.out.println(Arrays.toString(a));
    }

    public void mergeSort(int[] a,int left,int right,int[] temp){
        if(left>=right)
            return;
        int mid=left+(right-left)/2;
        mergeSort(a,left,mid,temp);
        mergeSort(a,mid+1,right,temp);
        merge(a,left,mid,right,temp);
    }

    private void merge(int[] a,int left,int mid,int right,int[] temp){
        int lp=left,rp=mid+1,tmp=left;
        while(lp<=mid&&rp<=right){
            if(a[lp]<a[rp]) temp[tmp++]=a[lp++];
            else temp[tmp++]=a[rp++];
        }
        while(lp<=mid) temp[tmp++]=a[lp++];
        while(rp<=right) temp[tmp++]=a[rp++];
        tmp=left;
        while(tmp<=right) a[tmp]=temp[tmp++];
    }





}
